let $ = require("jquery/dist/jquery.min");
class WxShare {
  constructor(config = {}) {
    this.INTERFACE_GETJSSIGN = '//bes.caitong.sina.com.cn/gxfc/wechat/getJsSign';
    this.jsApiList = ['onMenuShareTimeline', 'onMenuShareAppMessage', 'hideMenuItems', 'hideAllNonBaseMenuItem'];
    // ['onMenuShareTimeline', 'onMenuShareAppMessage', 'hideMenuItems']需要注册的JSAPI事件
    this.hideMenuItems = ['menuItem:share:qq', 'menuItem:share:weiboApp', 'menuItem:share:QZone', 'menuItem:share:facebook', 'menuItem:editTag', 'menuItem:delete', 'menuItem:copyUrl', 'menuItem:originPage', 'menuItem:readMode', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari', 'menuItem:share:email', 'menuItem:share:brand',
    // 'menuItem:share:appMessage','menuItem:share:timeline',
    'menuItem:favorite','menuItem:openWithQQBrowser', "menuItem:openWithSafari","menuItem:originPage","menuItem:copyUrl"
  ] //要隐藏的菜单项
    this.showShare = true;
    this.config={
      link:"//bes.caitong.sina.com.cn/index/render?__t__=member_share_transfer",
      title: '福彩3d模拟投注',
      desc: '广西福彩',
      imgUrl: 'http://n.sinaimg.cn/sports/caitong/gxfc/base/i/share_logo.png',
      ...config
    };
    
  }
  isWeixin() {
    var UA = navigator.appVersion;
    var isWeixin = false;
    var a = UA.toLowerCase();
    if (a.match(/MicroMessenger/i) == "micromessenger") {
      isWeixin = true
    } else {
      isWeixin = false
    }
    return isWeixin;
  }
  log() {
    if (console && console.log) {
      console.log.apply(console, arguments);
    }
  }
  getJsSign() {
    var self = this;
    var url = location.href.split('#')[0];

    $.ajax({
      type: 'GET',
      url: self.INTERFACE_GETJSSIGN,
      data: {
        'format': 'json',
        'url': url
      },
      dataType: 'json',
      cache: false,
      jsonpCallback: 'getJsSign',
      success: function (data) {
        var result = data.result;
        var code = result.status.code;
        if (code === 0) {
          var rdata = result.data;
          console.log("getJsSign:");
          self.log(rdata);
          self._config = rdata;
          if (rdata.sign) {
            self.wxInit();
          }
        }
      },
      error: function () {},
      complete: function (data) {
      }
    });
  }
  wxReady() {
    var self = this;
    wx.ready(function () {
      wx.checkJsApi({
        jsApiList: self.jsApiList, // 需要检测的JS接口列表，所有JS接口列表见附录2,
        success: function (res) {
          console.log(res);
        }
      });
      if (!self.showShare) {
        wx.hideAllNonBaseMenuItem();
        return
      }
      self.share();
    });
    wx.error(function (res) {
      self.log(res);
    });
  }
  wxInit() {
    var self = this;
    var config = self._config;

    wx.config({
      debug: false, //  开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
      appId: config.appId, //  必填，公众号的唯一标识
      timestamp: config.signature.timestamp, //  必填，生成签名的时间戳
      nonceStr: config.signature.noncestr, //  必填，生成签名的随机串
      signature: config.sign, //  必填，签名，见附录1
      jsApiList: self.jsApiList //  必填，需要使用的JS接口列表，所有JS接口列表见附录2
    });

    self.wxReady();
  }
  share() {
    var self = this;
    var obj = this.config;
    var meta = document.getElementsByTagName('meta');
    var share_desc = '';

    for (var i in meta) {
      if (typeof meta[i].name != "undefined" && meta[i].name.toLowerCase() == "description") {
        share_desc = meta[i].content;
      }
    }

    var title = obj.title || document.title;
    var desc = obj.desc || share_desc;
    var link = obj.link || location.href;
    var imgUrl = obj.imgUrl;

    wx.onMenuShareAppMessage({
      title: title, // 分享标题
      desc: desc, // 分享描述
      link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: imgUrl, // 分享图标
      success: function () {
        // 用户确认分享后执行的回调函数
        obj && obj.success && obj.success();
      },
      cancel: function () {
        // 用户取消分享后执行的回调函数
        obj && obj.cancel && obj.cancel();
      }
    });

    wx.onMenuShareTimeline({
      title: title, // 分享标题
      desc: desc, // 分享描述
      link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: imgUrl, // 分享图标
      success: function () {
        // 用户确认分享后执行的回调函数
        obj && obj.success && obj.success();
      },
      cancel: function () {
        // 用户取消分享后执行的回调函数
        obj && obj.cancel && obj.cancel();
      }
    });

    wx.hideMenuItems({
      menuList: self.hideMenuItems // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
    });
  }
  init() {
    var self = this;
    console.log(this.isWeixin());
    this.isWeixin() && self.getJsSign();
  }
}
module.exports = WxShare;